package net.esj.basic.service.impl;

import java.util.List;

import javax.annotation.PostConstruct;

import net.esj.basic.core.view.tools.searcher.SearcherUtils;
import net.esj.basic.dao.DaoProxy;
import net.esj.basic.dao.QueryBuilderDao;
import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.dao.querybuilder.QueryBuilderFactory;
import net.esj.basic.exception.MelonRuntimeException;
import net.esj.basic.service.QueryBuilderService;
import net.esj.basic.utils.Pagination;

public abstract class QueryBuilderServiceImpl extends ServiceSupport implements QueryBuilderService {

	protected QueryBuilderDao queryBuilderDao;
	
	@Override
	public int queryCountByQueryBuilder(QueryBuilder qb) {
		return queryBuilderDao.queryCountByQueryBuilder(qb);
	}

	@Override
	public <T> List<T> queryListByQueryBuilder(QueryBuilder qb, Class<T> clazz) {
		return (List) autoTransVo(queryBuilderDao.queryListByQueryBuilder(qb),clazz);
	}

	@Override
	public List queryListByQueryBuilder(QueryBuilder qb) {
		return queryBuilderDao.queryListByQueryBuilder(qb);
	}

	@Override
	public <T> Pagination queryPageByQueryBuilder(Pagination page,
			Class<T> clazz) {
		doQuerySearcher(page);
		return (Pagination) autoTransVo(queryBuilderDao.queryPageByQueryBuilder(page),clazz);
	}

	/**
	 * 分页查询并resolve查询信息(search queryMap)
	 */
	@Override
	public Pagination queryPageByQueryBuilder(Pagination page) {
		if(page.getQueryBuilder()==null && page.getEntityClass()!=null){
			page.setQueryBuilder(createQueryBuilder(page.getEntityClass()));
		}else if(page.getQueryBuilder()==null ){
			throw new MelonRuntimeException("queryBuilder 不存在");
		}
		doQuerySearcher(page);
		return queryBuilderDao.queryPageByQueryBuilder(page);
	}
	
	/**
	 * 分页查询并resolve查询信息(search queryMap)
	 */
	@Override
	public Pagination queryPageByQueryBuilder(Pagination page,boolean resolveSearcher) {
		if(page.getQueryBuilder()==null && page.getEntityClass()!=null){
			page.setQueryBuilder(createQueryBuilder(page.getEntityClass()));
		}else if(page.getQueryBuilder()==null ){
			throw new MelonRuntimeException("queryBuilder 不存在");
		}
		if(resolveSearcher){
			doQuerySearcher(page);
		}
		return queryBuilderDao.queryPageByQueryBuilder(page);
	}

	@Override
	public <T> List<T> queryTopByQueryBuilder(QueryBuilder qb, int count,
			Class<T> clazz) {
		return (List) autoTransVo(queryBuilderDao.queryTopByQueryBuilder(qb, count),clazz);
	}

	@Override
	public List queryTopByQueryBuilder(QueryBuilder qb, int count) {
		return queryBuilderDao.queryTopByQueryBuilder(qb, count);
	}
	
	
	@Override
	public <T> T queryFirst(QueryBuilder qb, Class<T> clazz) {
		return (T) autoTransVo(queryBuilderDao.queryFirst(qb), clazz);
	}

	@Override
	public Object queryFirst(QueryBuilder qb) {
		return queryBuilderDao.queryFirst(qb);
	}
	
	@Override
	public Object queryMaxByQueryBuilder(QueryBuilder qb, String key) {
		return queryBuilderDao.queryMaxByQueryBuilder(qb,key);
	}
	
	@Override
	public Long querySumByQueryBuilder(QueryBuilder qb, String key) {
		return queryBuilderDao.querySumByQueryBuilder(qb,key);
	}

	@Override
	public QueryBuilder createQueryBuilder(Class clazz) {
		return QueryBuilderFactory.create(clazz);
	}

	@Override
	public <T extends QueryBuilder> QueryBuilder createQueryBuilder(
			Class<T> qbClass, Class clazz) {
		return QueryBuilderFactory.create(qbClass,clazz);
	}

	@PostConstruct
	public void afterPropertiesSet() throws Exception {
		queryBuilderDao = DaoProxy.getProxy(QueryBuilderDao.class);		
	}

	protected void doQuerySearcher(Pagination pagination){
		SearcherUtils.pageQueryBuilder(pagination);
		/*Searcher searcher = pagination.getSearcher();
		if(searcher==null){
			return;
		}
		QueryBuilder qb = pagination.getQueryBuilder();
		for(SearcherColumn column : searcher.getColumns()){
			Object value = column.getValue();
			SymbolExpression operator  = SymbolExpression.getValueWithException(column.getType());
			String queryname = column.getKey();

			SymbolExpression.append(qb, operator, queryname, value);
		}
		for(SearcherColumn order : searcher.getOrders()){
			String key = order.getKey();
			String value = order.getValue().toString();
			OrderExpression operator ;
			try{
				operator = OrderExpression.getValueWithException(value);
			}catch(NoSuchElementException e){
				operator = OrderExpression.DESC;//默认就是equal
			}
			OrderExpression.append(qb, operator, key);
		}*/
	}
}
